import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';
import { DatePipe } from '@angular/common';
import setHours from 'date-fns/setHours';
import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
import { DisabledTimeFn, DisabledTimePartial } from "ng-zorro-antd/date-picker/standard-types";


@Component({
  selector: 'app-paijian',
  templateUrl: './paijian.component.html',
  styleUrls: ['./paijian.component.scss'],
  providers: [DatePipe]
})
export class PaijianComponent implements OnInit {
  listOfData = []; // 表格数据

  addDelivery = false; // 添加治具派件弹框
  startValue; // 開始时间
  endValue; // 結束時間
  twoOk = false; // 确认添加领取治具弹框
  union_order; // 工联单号
  deliveryUnionorder;

  // 日期只能选择今天及以后
  today = new Date();
  timeDefaultValue = setHours(new Date(), 0);

  // isOkLoadingTime;

  constructor(
    public axios: AxiosService,
    private datePipe: DatePipe,
  ) { }

  ngOnInit(): void {
    this.getData();
  }

  disabledDate = (current: Date): boolean => {
    // Can not select days before today and today
    return differenceInCalendarDays(current, this.today) < 0;
  };

  // 查询工单信息
  getData() {
    this.axios.axiosPost('QueryDeliveryOrder')
      .then((data) => {
        if (data) {
          console.log(data);
          this.listOfData = data['order'];
          console.log(this.listOfData);
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }

  // 显示添加派件时间弹框
  delivery(item) {
    this.union_order = item.union_order;
    this.startValue = item.starttime;
    this.endValue = item.endtime;
    this.deliveryUnionorder = item.union_order;
    this.addDelivery = true;
  }
  // 取消
  CancelDelivery() {
    this.addDelivery = false;
  }
  // 确认派件时间
  DeliveryOk() {
    if (this.startValue !== 'null' && this.endValue !== 'null' && this.startValue != '' && this.endValue != '') {
      this.addDelivery = false;
      this.twoOk = true;
    } else {
      alert('派件時間不能為空');
      this.addDelivery = true;
      this.twoOk = false;
    }
  }
  // 二次确认 取消
  twoOkCancel() {
    this.twoOk = false;
  }
  // 二次确认 确认
  TwoOk() {
    this.twoOk = false;
    console.log(this.union_order, this.startValue, this.endValue)
    this.axios.axiosPost('UpdateDeliveryOrder', { 'unionorder': this.union_order, name1: 'starttime', newValue1: this.startValue, name2: 'endtime', newValue2: this.endValue })
      .then((data) => {
        console.log(data)
        debugger
        if (data['result'] === 'ok') {
          console.log(data);
          // 派送完改变工单的状态
          this.axios.axiosPost('UpdateUnionOrder', { 'unionorder': this.union_order, name: 'status', newValue: 1 })
            .then((data) => {
              if (data['result'] === 'ok') {
                console.log(data);
              }
            })
            .catch((err) => {
              console.log(err);
            })
          this.getData();
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }

  // 选择日期
  onStartChange(date: Date): void {
    this.startValue = date;
    this.startValue = this.datePipe.transform(this.startValue, 'yyyy-MM-dd HH:mm:ss');
    console.log(this.startValue)
  }
  onEndChange(date: Date): void {
    this.endValue = date;
    this.endValue = this.datePipe.transform(this.endValue, 'yyyy-MM-dd HH:mm:ss');
    console.log(this.endValue)
  }

}
